﻿using System;
using System.Text;
using Notepad_X.Components;
using ICSharpCode.TextEditor.Document;
namespace Notepad_X
{

    public class Document
    {
        private bool avail = true;
        public int id;
        public bool saved = true;
        public string path = null;
        public TabPageX tab_page = new TabPageX(-1);
        public System.IO.FileSystemWatcher watcher;
        public bool watcherChanged;
        public ICSharpCode.TextEditor.TextEditorControl text_box = new ICSharpCode.TextEditor.TextEditorControl();
        public int find_pos;
        public int find_index;
        public string find_string;
        /// <summary>
        /// Document constructor
        /// </summary>
        /// <param name="_id">The document index</param>
        public Document(int _id)
        {
            id = _id;
            avail = false;
            saved = true;
            path = null;
            tab_page.Controls.Add(text_box);
            tab_page.id = id;
            tab_page.Text = id.ToString();
            tab_page.AllowDrop = true;
            text_box.Tag = id;
            text_box.Dock = System.Windows.Forms.DockStyle.Fill;
            text_box.Location = new System.Drawing.Point(0, 0);
            text_box.ShowEOLMarkers = false;
            text_box.ShowInvalidLines = false;
            text_box.ShowSpaces = false;
            text_box.ShowLineNumbers = false;
            text_box.ShowHRuler = false;
            text_box.ShowVRuler = false;
            text_box.EnableFolding = false;
            text_box.ActiveTextAreaControl.TextArea.AllowDrop = true;
            text_box.AllowDrop = true;
            //text_box.DragOver += new System.Windows.Forms.DragEventHandler(text_box_DragOver);
            //text_box.DragDrop += new System.Windows.Forms.DragEventHandler(text_box_DragDrop);

            //text_box.Document.HighlightingStrategy = HighlightingManager.Manager.DefaultHighlighting;

            watcherChanged = false;
            find_pos = -1;
            find_index = -1;
            find_string = null;
        }

        /// <summary>
        /// Clear the document
        /// </summary>
        public void clear()
        {
            avail = true;
            id = -1;
            saved = true;
            path = null;
            tab_page.Dispose();
            text_box.Dispose();
            tab_page = new TabPageX(-1);
            text_box = new ICSharpCode.TextEditor.TextEditorControl();
            watcher = null;
        }
        /// <summary>
        /// Set the document file path and set it as saved
        /// </summary>
        /// <param name="gpath">file path</param>
        public void save(String gpath)
        {
            path = gpath;
            set_saved();
        }

        /// <summary>
        /// Set the document as unsaved
        /// </summary>
        public void set_unsaved()
        {
            saved = false;
            tab_page.ImageKey = "documentchanged";
        }

        /// <summary>
        /// Set the document as saved
        /// </summary>
        public void set_saved()
        {
            saved = true;
            tab_page.ImageKey = "documentok";
        }
        /// <summary>
        /// Get if the document is available
        /// </summary>
        /// <returns></returns>
        public bool get_empty()
        {
            return avail;
        }

        public void Wrap()
        {
            text_box.Text = Wrap(text_box.Text, 50);
        }
        
        public static string Wrap(string text, int max)
        {
            return System.Text.RegularExpressions.Regex.Replace(text, @"(^| +)([^\r\n]{0," + max + @"}(?![\w\p{P}]))", "$2\r\n", System.Text.RegularExpressions.RegexOptions.Multiline);
        } 
        /// <summary>
        /// Get if the document if saved or not
        /// </summary>
        /// <returns></returns>
        public bool get_saved()
        {
            return saved;
        }

    }

    public class DocumentCollection : System.Collections.ArrayList
    {
        public DocumentCollection(){}

        public Document this[int index]
        {
            get { return (Document)base[index]; }
            set { base[index] = value; }
        }
        /// <summary>
        /// Add document to the collection
        /// </summary>
        /// <param name="doc">The document</param>
        /// <returns>Returns the index id</returns>
        public int Add(Document doc)
        {
            return base.Add(doc);
        }
    }
}
